
function SDOrefreshAlerts() {
    var inf = document.getElementById ("po_sdo_alerts_info");
    if (inf) inf.textContent = "checking...";
    top.po_sdo_refresh (true, SDOinstallAlertsOverview);
}


function SDOinstallAlertsOverview () {
    // Add the control panel for sighting reporting.
    var status = document.getElementById ("status");
    if (!status) return;

    // Check if the stuff is already installed. If so, delete it first
    var tr = document.getElementById ("po_sdo_alert_overview");
    if (tr) tr.parentNode.removeChild (tr);
    // Create a new row in the Status table and insert it
    tr = document.createElement ("tr");
    tr.id = "po_sdo_alert_overview";
    status.firstChild.insertBefore (tr, status.firstChild.lastChild);
    td = status.firstChild.childNodes[1].firstChild.cloneNode(false);
    tr.appendChild (td);
    
    // Read the alerts info and create the alerts string
    var sector = POgetSector();
    var info = localStorage.getItem ('po_sdo_alerts');
    var local = localStorage.getItem ('po_sdo_alerts_local');
    var infostr = null;
    if (local) {
        var div = document.createElement ("div");
        div.innerHTML = local;
        if (div.lastChild.getAttribute("sector") == sector) {
            div.removeChild (div.lastChild);
            infostr = div.innerHTML;
//POlog ("SDOinstallAlertsOverview: Using local");
        } else {
            localStorage.removeItem ('po_sdo_alerts_local');
        }
    }
    if (!infostr && info) {
        var div = document.createElement ("div");
        div.innerHTML = info;
        for (var i=0; i<div.firstChild.childNodes.length; i++) {
            if (div.firstChild.childNodes[i].getAttribute ("sector") == sector) {
                var alerts = Number (div.firstChild.childNodes[i].getAttribute ("alerts"));
                var color = null;
                if (alerts < 3) color = "lime";
                else if (alerts < 5) color = "orange";
                else color = "red";
                infostr = "<span><a id='po_sdo_alerts_info' style='color:"+color+";font-weight:bold' href='javascript:SDOrefreshAlerts();void(0);'>"+alerts+" alerts</a>\
                          &nbsp<a href=\"javascript:SDOshowAlertDetails();void(0);\">(details)</a></span>";
                var temp = document.createElement ("div");
                temp.appendChild (div.firstChild.childNodes[i]);
                localStorage.setItem ('po_sdo_alerts_local', infostr+temp.innerHTML);
//POlog ("SDOinstallAlertsOverview: Creating new");
                break;
            }
        }
    }
    if (!infostr) {
        infostr = "<span><a id='po_sdo_alerts_info' href='javascript:SDOrefreshAlerts();void(0);' style='color:gray;font-weight:bold'>"+sector+": no info available</a></span>";
    }
    
    // Add the stuff inside
    td.innerHTML = "<span style='margin: 0pt 20px;' id='po_sdo_alert_link'>"+infostr+"</span>";
    
    // Make the colored box around NAV area
    if (localStorage.getItem ("po_sdo_show_nav_frame")=="true") {
        var nav = SDOnavArea();
        if (!nav) return;
        if (POisPartialRefresh()) 
            nav = nav.parentNode.parentNode.parentNode.parentNode;
        else
            nav = nav.parentNode.parentNode;
        color = td.firstChild.firstChild.firstChild.style.color;
        nav.setAttribute ("style", "border: 2px solid "+color+";");
    }
    if (localStorage.getItem ("po_sdo_show_overlay")=="true") {
        SDOshowOverlay ();
    }
}

function SDOnavArea () {
    var nav = null;
    if (POisPartialRefresh()) {
        var nava = document.getElementById ("nav");
        var navt = document.getElementById ("navtransition");
        if (nava) {
            if (nava.style.display == "block"){
                nav = nava.firstChild.lastChild;
            }
        }
        if (navt) {
            if (navt.style.display == "block"){
                nav = navt.firstChild.firstChild;
            }
        }
    } else {
        nav = document.getElementById ("navarea").lastChild;
    }
    return nav;
}

function SDOshowOverlay () {
    var local = localStorage.getItem ('po_sdo_alerts_local');
    if (!local) return;
    var screenSize = 0;
    var nav = SDOnavArea ();
    var loc = POgetCoords();
    var sec = POgetSector();
    var currCol = Number(loc.split(",")[0].substr(1));
    var currRow = Number(loc.split(",")[1].split("]")[0]);
    
    var alerts = document.createElement ("div");
    alerts.innerHTML = local;
    alerts.removeChild (alerts.firstChild);
    alerts = alerts.firstChild;
    
    if (POisPartialRefresh()) {
        screenSize = window.navAreaSize;
    } else {
        screenSize = nav.childNodes.length / 2;
    }
    var halfScreen = ((screenSize-1)/2)+1;
    
    // Loop over the tiles and show the overlay info in case there's an alert set on that tile.
    // Clear the others in case there's no alert defined
    for (i=0; i<alerts.childNodes.length; i++) {
        var alert = alerts.childNodes[i];
        var title = alert.getAttribute ("title");
        var rowCoord = Number(alert.getAttribute("ycoord"));
        var colCoord = Number(alert.getAttribute("xcoord"));
        rowCoord = (rowCoord - currRow + halfScreen - 1);
        colCoord = (colCoord - currCol + halfScreen - 1);
        if (rowCoord < 0 || colCoord < 0 || rowCoord >= screenSize || colCoord >= screenSize) {
            continue;
        }
        var row = SDOgetChild (nav, rowCoord, "tr");
        var cell = SDOgetChild (row, colCoord, "td");
//POlog ("SDOshowOverlay: Alert: "+alert.textContent+" X:"+colCoord+" Y:"+rowCoord );
        
        var type = alert.getAttribute ("type").toLowerCase();
        var color = type=="safe"?"lime":"red";
        var text = type=="safe"?"S":"A";
        //cell.style.border = "1px lime dotted";

        // OK, we have our tile. Let's make the overlay now.
        // Check if the tile is untouched or if there already is a table added by another script
        var lnk = cell.firstChild;
        var img = lnk.firstChild;
        var tbl = document.createElement ("table");
        tbl.setAttribute ("width", "64");
        tbl.setAttribute ("height", "64");
        tbl.setAttribute ("cellspacing", "0");
        tbl.setAttribute ("cellpadding", "0");
        tbl.setAttribute ("style", "border: 1px solid "+color+";");
        tbl.innerHTML = "<tbody><tr><td style='vertical-align: top;color:"+color+";font-weight: bold;font-size: large' title='"+title+"'>"+text+"</td></tr></tbody>";
        if (lnk.tagName.toLowerCase() == "a" && img.tagName.toLowerCase() == "img") {
            tbl.setAttribute ("background", img.getAttribute ("src"));
            
            lnk.removeChild(img);
            lnk.appendChild (tbl);
        } else if (lnk.tagName.toLowerCase() == "img") {
            tbl.setAttribute ("background", lnk.getAttribute ("src"));
            cell.removeChild (lnk);
            cell.appendChild (tbl);
        } else if (lnk.tagName.toLowerCase() == "a" && img.tagName.toLowerCase() == "table") {
            tbl.setAttribute ("background", img.getAttribute ("background"));
            lnk.removeChild (img);
            lnk.appendChild (tbl);
        }

    }
}

// Finds and returns n-th tagName child in the given parent
function SDOgetChild (parent, n, tagName) {
    var children = parent.childNodes;
    var x = 0;
    for (var i=0; i<children.length; i++) {
        var aChild = children[i];
        var tag = aChild.tagName;
        if (tag == undefined) continue;
        if (aChild.tagName.toLowerCase() == tagName) {
            if (x==n) return aChild;
            x ++;
        }
    }
    return null;
}

function SDOshowAlertDetails () {
    // Chack if the pamel is already open and close it if so.
    var panel = document.getElementById ("po_sdo_alert_panel");
    if (panel) {
        panel.parentNode.removeChild (panel);
        return;
    }
    
    var span = document.getElementById ("po_sdo_alert_link");
    var local = localStorage.getItem ('po_sdo_alerts_local');
    if (!span || !local) return;
    
    panel = document.createElement ("div");
    panel.id = "po_sdo_alert_panel";
    panel.setAttribute ("style", 'margin: 0pt 25px;');
    panel.innerHTML = local;
    panel.removeChild (panel.firstChild);
    span.appendChild (panel);
    
    
}

function SDOshowHack(hackId) {
    var leftPos = 0;
    var topPos = 0;
    if (screen) {
            leftPos = (screen.width / 2) - 375;
            topPos = (screen.height / 2) - 275;
    }
    var url = POgetServerUrl ("hack_details.php", false);
    window.open(url+"?id=" + hackId, "_blank", "width=750,height=550,scrollbars=1,resizable=1,left=" + leftPos + ",top=" + topPos);
}

function SDOshowCombat(id) {
    var leftPos = 0;
    var topPos = 0;
    if (screen) {
            leftPos = (screen.width / 2) - 375;
            topPos = (screen.height / 2) - 275;
    }
    var url = POgetServerUrl ("combat_details.php", false);
    window.open(url+"?id=" + id, "_blank", "width=750,height=550,scrollbars=1,resizable=1,left=" + leftPos + ",top=" + topPos);
}
        
// Make sure it will work with PartialRefresh
function SDOhijackStatus () {
    var local_updateStatus = window.updateStatus;
    if (local_updateStatus != undefined) {
        window.updateStatus = function(cmd) {
            local_updateStatus(cmd);
            SDOinstallAlertsOverview ();
        }
    }
}

window.onload = setTimeout(function () {
    SDOinstallAlertsOverview ();
    SDOhijackStatus ();
}, 1);


